package com.my.basic.flow.backpressure;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SubmissionPublisher;

/**
 * @author: marshal
 * @date: 2022-07-27 16:25
 */
public class BackpressureTest {
    public static void main(String[] args) throws Exception {
        // 定义有一个线程的线程池，使生产者和消费者在两个线程上工作
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        // SubmissionPublisher第二个参数控制缓存数据的个数（内部有个计算的公式）
        SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>(executorService, 10);
        System.out.println("getMaxBufferCapacity: " + publisher.getMaxBufferCapacity());

        MySubscriber subscriber = new MySubscriber();
        publisher.subscribe(subscriber);
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName() + " |  publish data : " + i);
            publisher.submit(i);
        }

        publisher.close();
        executorService.shutdown();

        Thread.sleep(10000);
    }
}
